package com.swgk.core.util;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.util.Pair;

import com.github.dfqin.grantor.PermissionListener;
import com.github.dfqin.grantor.PermissionsUtil;
import com.swgk.core.R;
import com.swgk.core.base.api.APIManager;
import com.swgk.core.base.aroute.AppRouteConstant;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.utils.ActivityJump;
import com.swgk.core.dialog.RoundCornerDialog;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

//认证，管理员权限弹框
public class ACAdminDialogUtils {
    public static final int TYPE_NORMAL = -1;//默认
    public static final int TYPE_NO_LOGIN = 0;//未登录
    public static final int TYPE_NO_PERSON_AUTH = 3;//未认证
    public static final int TYPE_YES_PERSON_AUTH = 4;//已认证
    public static final int TYPE_NO_AC = 1;//没有认证
    public static final int TYPE_NO_PERMISSION = 2;//无权限
    public static final int TYPE_YES_PERSON_AUTH_NO_AC = 5;//无权限
    public static final int TYPE_AUTH_NO_AC = 6;//无实名认证
    public static final int TYPE_NO_OPERATE_PERMISSION = 7; //无操作权限
    public static final int TYPE_AUTH_NO_NEW_AC = 8;//无实名认证

    public static final int TYPE_MENU_CHANGE = 9;//菜单栏数据变更
    public static final String MK_ADDCOMMODITY = "addCommodity";//商品发布权限key
    public static final String MK_OURINQUIRYPRICELIST = "2_wodexunjia";//我的询价权限key
    public static final String MK_3_PINPAIGUANLI = "3_pinpaiguanli";//品牌管理key
    public static final String MK_3_CANGCHUGUANLI = "3_cangchuguanli";//仓储管理权限key
    public static final String MK_10_HUOYUANGUANLI = PermissionCheckUtil.KEY_HUOYUANGUANLI;//货源管理权限key
    public static final String MK_10_CHENGYUNSHANGJI = PermissionCheckUtil.KEY_CHENGYUNSHANGJI;//承运管理权限key
    public static final String MK_14_YAOQINGHUIYUAN = "会员经营";//会员key
    public static final String MK_7_TOUBIAOCHAXUN = "7_toubiaochaxun";//招采 投标查询
    public static final String MK_7_JINGJIACHAXUN = "7_jingjiachaxun";//招采- 竞价查询
    private static DialogClick mDialogClick;

    public static void setmDialogClick(DialogClick mDialogClick) {
        ACAdminDialogUtils.mDialogClick = mDialogClick;
    }

    public static int isPermission(String key, boolean isChildMenu) {
        int type = TYPE_NORMAL;
        if (TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
            type = TYPE_NO_LOGIN;
        } else if (!"SUCCESS".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.ENTERPRISE_AUTH, "")) && !"1".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.RELEVANCE, ""))) {
            type = TYPE_NO_AC;
        } else {
            if (isChildMenu) {
                if (!SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.CHILD_MENU, "").contains(key)) {
                    type = TYPE_NO_PERMISSION;
                }
            } else {
                if (!SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.IS_DATA, "").contains(key)) {
                    type = TYPE_NO_PERMISSION;
                }
            }
        }
        return type;
    }

    public static int isPermissionForPublish(String key, boolean isChildMenu) {
        int type = TYPE_NORMAL;
        if (TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
            type = TYPE_NO_LOGIN;
        } else if (!"SUCCESS".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.ENTERPRISE_AUTH, "")) && !SharedPreferenceUtil.getInstance().getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH, false)) {
            type = TYPE_NO_PERSON_AUTH;
        } else if (!"SUCCESS".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.ENTERPRISE_AUTH, "")) && !SharedPreferenceUtil.getInstance().getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH, false)) {
            type = TYPE_YES_PERSON_AUTH;
        } else if (!"SUCCESS".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.ENTERPRISE_AUTH, "")) && !"1".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.RELEVANCE, ""))) {
            type = TYPE_NO_AC;
        } else {
            if (isChildMenu) {
                if (!SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.CHILD_MENU, "").contains(key)) {
                    type = TYPE_NO_PERMISSION;
                }
            } else {
                if (!SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.IS_DATA, "").contains(key)) {
                    type = TYPE_NO_PERMISSION;
                }
            }
        }
        return type;
    }

    public static int isPermissionForAsk(String key, boolean isChildMenu) {
        int type = TYPE_NORMAL;
        if (TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
            type = TYPE_NO_LOGIN;
        } else if (!SharedPreferenceUtil.getInstance().getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH, false)) {
            type = TYPE_NO_PERSON_AUTH;
        } else if (!"SUCCESS".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.ENTERPRISE_AUTH, "")) && !"1".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.RELEVANCE, ""))) {
            type = TYPE_NO_AC;
        } else {
            if (isChildMenu) {
                String s = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.CHILD_MENU, "");
                if (!SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.CHILD_MENU, "").contains(key)) {
                    type = TYPE_NO_PERMISSION;
                }
            } else {
                if (!SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.IS_DATA, "").contains(key)) {
                    type = TYPE_NO_PERMISSION;
                }
            }
        }
        return type;
    }

    public static void showACDialog(int type, Context context) {
        String confirm = "确定";
        String content = "";
        if (type == TYPE_NO_LOGIN) {
            confirm = "去登录";
            content = "请登录后再试";
        } else if (type == TYPE_NO_AC) {
            confirm = "去认证";
            content = "请认证企业后再试";
        } else if (type == TYPE_NO_PERMISSION) {
            confirm = "确定";
            content = context.getString(R.string.this_account_has_no_permission);
        } else if (type == TYPE_AUTH_NO_AC) {
            confirm = "去认证";
            content = "请先进行个人认证，才可使用该功能";
        } else if (type == TYPE_NO_OPERATE_PERMISSION) {
            confirm = "确定";
            content = "该账号无操作权限";
        } else if (type == TYPE_AUTH_NO_NEW_AC) {
            confirm = "确定";
            content = "你还没有完成实名认证，请先完成个人实名认证再进行其他认证";
        } else if (type == TYPE_MENU_CHANGE) {
            confirm = "设置首页";
            content = "抱歉，由于经营帮业务变动，顶部菜单已恢复为默认排序，为您带来不便，请见谅";
        }
        View view = View.inflate(context, R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(context, 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失

        TextView tv_confirm = (TextView) view.findViewById(R.id.tv_logout_confirm);
        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        tv_title.setVisibility(View.VISIBLE);
        TextView tv_logout_cancel = (TextView) view.findViewById(R.id.tv_logout_cancel);
        tv_title.setText("温馨提示");
        tv_confirm.setText(confirm);
        tv_message.setText(content);
        //确定
        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type == TYPE_NO_LOGIN) {
//                    EventBus.getDefault().postSticky("transport_to_login");
                    ActivityJump.startActivity(AppRouteConstant.VerificationCodeLoginActivity, Pair.create("isJump", false));
                } else if (type == TYPE_NO_AC) {
                    requestAuthWriteStorage(context);
                } else if (type == TYPE_AUTH_NO_AC) {
                    requestAuthWriteStorage(context);
                } else if (type == TYPE_AUTH_NO_NEW_AC) {
                    requestAuthWriteStorage(context);
                } else if (type == TYPE_MENU_CHANGE) {
                    ActivityJump.startActivity(AppRouteConstant.PreferredChoiceActivity, Pair.create("isFirst", false));
                }
                roundCornerDialog.dismiss();
            }
        });

        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }

    public static void showACDialogForGoods(final int type, Context context) {
        String confirm = "确定";
        String content = "";
        if (type == TYPE_NO_LOGIN) {
            confirm = "去登录";
            content = "请登录后再试";
            ActivityJump.startActivity(AppRouteConstant.VerificationCodeLoginActivity, Pair.create("isJump", false));
            return;
        } else if (type == TYPE_NO_PERSON_AUTH) {
            confirm = "去认证";
            content = "请认证或加入企业后再试";
        } else if (type == TYPE_YES_PERSON_AUTH) {
            confirm = "去处理";
            content = "请认证或加入企业后再试";
        } else if (type == TYPE_NO_AC) {
            confirm = "去认证";
            content = "请把身份切换到企业或者加入已认证的企业后再试";
        } else if (type == TYPE_NO_PERMISSION) {
            MToast.showToast(context, "您没有权限，请联系管理员开通");
            return;
        } else if (type == TYPE_YES_PERSON_AUTH_NO_AC) {
            MToast.showToast(context, "您没有权限，请联系管理员开通");
            return;
        }

        View view;
        if (type == TYPE_NO_AC || type == TYPE_NO_PERSON_AUTH) {
            view = View.inflate(context, R.layout.dialog_round_three_button, null);
        } else {
            view = View.inflate(context, R.layout.dialog_two_btn, null);
        }

        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(context, 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失

        TextView tv_message = view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm;
        if (type == TYPE_NO_AC || type == TYPE_NO_PERSON_AUTH) {
            tv_logout_confirm = view.findViewById(R.id.tv_confirm);
        } else {
            tv_logout_confirm = view.findViewById(R.id.tv_logout_confirm);
        }
        tv_title.setVisibility(View.VISIBLE);
        TextView tv_logout_cancel;
        if (type == TYPE_NO_AC || type == TYPE_NO_PERSON_AUTH) {
            tv_logout_cancel = view.findViewById(R.id.tv_cancel);
        } else {
            tv_logout_cancel = view.findViewById(R.id.tv_logout_cancel);
        }

        if (type == TYPE_NO_AC || type == TYPE_NO_PERSON_AUTH) {
            TextView tvJoin = view.findViewById(R.id.tv_join);
            tvJoin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ActivityJump.startActivity(AppRouteConstant.JoinCompanyActivity);
                }
            });
        }
        tv_title.setText("您没有权限");
        tv_logout_confirm.setText(confirm);
        tv_logout_cancel.setText("我知道了");
        tv_message.setText(content);
        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type == TYPE_NO_LOGIN) {
                    ActivityJump.startActivity(AppRouteConstant.VerificationCodeLoginActivity, Pair.create("isJump", false));
                } else if (type == TYPE_NO_AC || type == TYPE_NO_PERSON_AUTH) {
                    ActivityJump.startActivity(AppRouteConstant.CertificationActivity);
                } else if (type == TYPE_NO_PERMISSION) {

                }
                roundCornerDialog.dismiss();
            }
        });

        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }

    public static void requestAuthWriteStorage(Context context) {
        if (PermissionsUtil.hasPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            ActivityJump.startActivity(AppRouteConstant.AuthActivity3);
        } else {
            PermissionsUtil.requestPermission((Activity) context, new PermissionListener() {
                @Override
                public void permissionGranted(@NonNull String[] permissions) {
                    ActivityJump.startActivity(AppRouteConstant.AuthActivity3);
                }

                @Override
                public void permissionDenied(@NonNull String[] permissions) {
                    Toast.makeText(context, "请先获相应的操作权限", Toast.LENGTH_LONG).show();
                }
            }, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
    }

    public static void showACDialogForPublish(final int type, Context context, DialogClick dialogClick) {
        String confirm = "确定";
        String content = "";
        if (type == TYPE_NO_LOGIN) {
            confirm = "去登录";
            content = "请登录后再试";
            ActivityJump.startActivity(AppRouteConstant.VerificationCodeLoginActivity, Pair.create("isJump", false));
            return;
        } else if (type == TYPE_NO_PERSON_AUTH) {
            confirm = "去认证";
            content = "请认证或加入企业后再试";
        } else if (type == TYPE_YES_PERSON_AUTH) {
            confirm = "去处理";
            content = "请加入或认证企业后再试";
        } else if (type == TYPE_NO_AC) {
            confirm = "切换身份";
            content = "请把身份切换到企业或者加入已认证的企业后再试";
        } else if (type == TYPE_NO_PERMISSION) {
            MToast.showToast(context, "您没有权限，请联系管理员开通");
            return;
        } else if (type == TYPE_AUTH_NO_AC) {
            confirm = "确定";
            content = "你还没有完成实名认证，请先完成个人实名认证再进行其他认证";
        }

        View view = View.inflate(context, R.layout.dialog_two_btn, null);

        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(context, 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失

        TextView tv_message = view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = view.findViewById(R.id.tv_logout_confirm);
        tv_title.setVisibility(View.VISIBLE);
        TextView tv_logout_cancel = view.findViewById(R.id.tv_logout_cancel);

        tv_title.setText("您没有权限");
        tv_logout_confirm.setText(confirm);
        tv_logout_cancel.setText("我知道了");
        tv_message.setText(content);
        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type == TYPE_NO_LOGIN) {

                } else if (type == TYPE_NO_PERSON_AUTH) {
                    dialogClick.dialogClick1();
                } else if (type == TYPE_YES_PERSON_AUTH) {
                    dialogClick.dialogClick2();
                } else if (type == TYPE_NO_AC) {
                    dialogClick.dialogClick3();
                } else if (type == TYPE_AUTH_NO_AC) {
                    requestAuthWriteStorage(context);
                }
                roundCornerDialog.dismiss();
            }
        });

        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }

    public static void showACBackDialog(int type, Context context, DialogClick dialogClick) {
        String confirm = "确定";
        String content = "";
        if (type == TYPE_MENU_CHANGE) {
            confirm = "设置首页";
            content = "抱歉，由于经营帮业务变动，顶部菜单已恢复为默认排序，为您带来不便，请见谅";
        }
        View view = View.inflate(context, R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(context, 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失

        TextView tv_confirm = (TextView) view.findViewById(R.id.tv_logout_confirm);
        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        tv_title.setVisibility(View.VISIBLE);
        TextView tv_logout_cancel = (TextView) view.findViewById(R.id.tv_logout_cancel);
        tv_title.setText("温馨提示");
        tv_confirm.setText(confirm);
        tv_message.setText(content);
        //确定
        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type == TYPE_MENU_CHANGE) {
                    dialogClick.dialogClick4();
                }
                roundCornerDialog.dismiss();
            }
        });

        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }

    /**
     * @param context
     * @param dialogClick
     * @param msg
     * @param title
     * @param buttonText
     */
    public static void showACDialogNew(Context context, String title, String msg, String buttonText, DialogClick dialogClick) {
        View view = View.inflate(context, R.layout.dialog_two_btn_circle, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(context, 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        // 设置点击屏幕Dialog不消失
        roundCornerDialog.setCanceledOnTouchOutside(false);
        TextView tv_message = view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = view.findViewById(R.id.tv_logout_confirm);
        TextView tv_logout_cancel = view.findViewById(R.id.tv_logout_cancel);
        tv_message.setText(msg);
        tv_title.setText(title);
        tv_logout_confirm.setText(buttonText);
        tv_title.setVisibility(View.VISIBLE);
        //确定
        tv_logout_confirm.setOnClickListener(v -> {
            if (dialogClick != null) {
                dialogClick.dialogClick1();
            }
            roundCornerDialog.dismiss();
        });
        //取消
        tv_logout_cancel.setOnClickListener(v -> roundCornerDialog.dismiss());
    }

    public interface DialogClick {
        default void dialogClick1() {

        }

        default void dialogClick2() {

        }

        default void dialogClick3() {

        }

        default void dialogClick4() {

        }
    }
}
